﻿using EGuayas.Web.Resources;
using EntGuayasSLA.BL;
using EntGuayasSLA.BL.Listas;
using EntGuayasSLA.BL.Listas.Criterias;
using Ext.Net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;

namespace EGuayas.Web
{
    public class pgBase : Page
    {
        public Boolean MostrarAlertaFaseNoAutorizada(string page, string fase, Boolean ifWrite)
        {
            Boolean valAutRol = false;
            if (GetSessionUsuario.UsuarioRolCodigo == Valores.Codigo_Rol_TecnicoAuditoria ||
                GetSessionUsuario.UsuarioRolCodigo == Valores.Codigo_Rol_TecnicoLicencimiento)
            {
                var idFaseLic = Session[page + fase].ToString();
                var tareaCriteria = ModuloCriteria.NewModuloCriteria();
                tareaCriteria.Id = idFaseLic;
                switch (page)
                {
                    case SessionPage.PageLicAdm: tareaCriteria.Modulo = Valores.Modulo_Tarea_Licenciamiento;
                        break;
                    case SessionPage.PageGestionAdminAudCumpEdit: tareaCriteria.Modulo = Valores.Modulo_Tarea_Aud_Cump;
                        break;
                    case SessionPage.PageGestionAdminAudMonEdit: tareaCriteria.Modulo = Valores.Modulo_Tarea_Aud_Moni;
                        break;
                    case SessionPage.PageGestionAdminAudPmaEdit: tareaCriteria.Modulo = Valores.Modulo_Tarea_Aud_Pma;
                        break;
                    case SessionPage.PageGestionAdminAudOtsEdit: tareaCriteria.Modulo = Valores.Modulo_Tarea_Aud_Otro;
                        break;
                }
                
                var listaTareas = TareaLista.GetTareaLista(tareaCriteria);
                var tarPer = listaTareas.FirstOrDefault(lt => lt.PersonaTecnioId == GetSessionUsuario.PersonaId);
                if (tarPer == null || tarPer.Id.ToString() == "")
                {
                    if (ifWrite)
                    {
                        var confMsg = new MessageBoxConfig();
                        confMsg.Modal = true;
                        confMsg.Closable = false;
                        confMsg.Title = Titulos.TituloAviso;
                        confMsg.Icon = MessageBox.Icon.WARNING;
                        confMsg.Message = "No esta autorizado para acceder a esta fase";
                        X.Msg.Show(confMsg);
                        /*Response.End();
                        Response.Write("No esta autorizado para acceder a esta fase");*/
                    }
                    
                    valAutRol = true;
                }
                
            }
            return (valAutRol);
        }

        public pgBase()
        {
        }
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);

            
            //It appears from testing that the Request and Response both share the 
            // same cookie collection.  If I set a cookie myself in the Reponse, it is 
            // also immediately visible to the Request collection.  This just means that 
            // since the ASP.Net_SessionID is set in the Session HTTPModule (which 
            // has already run), thatwe can't use our own code to see if the cookie was 
            // actually sent by the agent with the request using the collection. Check if 
            // the given page supports session or not (this tested as reliable indicator 
            // if EnableSessionState is true), should not care about a page that does 
            // not need session
            if (Context.Session != null)
            {
                //Tested and the IsNewSession is more advanced then simply checking if 
                // a cookie is present, it does take into account a session timeout, because 
                // I tested a timeout and it did show as a new session
                if (Session.IsNewSession)
                {
                    // If it says it is a new session, but an existing cookie exists, then it must 
                    // have timed out (can't use the cookie collection because even on first 
                    // request it already contains the cookie (request and response
                    // seem to share the collection)
                    string szCookieHeader = Request.Headers["Cookie"];
                    if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                    {                      
                        string url = HttpContext.Current.Request.Url.AbsoluteUri;
                        string pg = url.Substring(url.LastIndexOf('/') + 1);
                        if (pg != "pgLogin.aspx")
                        {
                           X.Js.Call("AppGlobalJs.controlSesion");//->Metodo Js que muestra alerta y redireciona al login
                        }
                    }
                }
            }
        }
        public void RegistrarLog(object log, bool mostrarVentana = false, string mensaje = "")
        {
            var rutaLog = MapPath("~/Resources/log.txt");
            if (ConfigurationManager.AppSettings.Get("Debug") == "true")
            {
                Window win = new Window
                {
                    ID = "winLog",Title = "Log",Height = 185,Width = 350,BodyPadding = 5,Modal = true,AutoScroll = true,CloseAction = CloseAction.Destroy,
                    Html = log.ToString()
                };
                win.Render();
                X.Js.Call("AppGlobalJs.showBug", log.ToString());
            }
            Util.RegistarLog(log, rutaLog);

            if (mostrarVentana)
            {
                X.Msg.Show(new MessageBoxConfig{Buttons = MessageBox.Button.OK,Icon = MessageBox.Icon.ERROR,Title = Titulos.TituloAviso,Message = mensaje});
            }
        }
        public static string ClaveIngreso(Usuarios user, string sTxtMail)
        {
            sTxtMail +=
                "<br/><br/>Los datos necesarios para su ingreso a la Plataforma PLASA del Gobierno Provincial del Guayas son:<br/><br/>";

            sTxtMail +=
                string.Format(
                    "<br/><b>A continuación <a href=\"{0}pgLogin.aspx\">Ingrese aquí</a> para ir a la Plataforma Tecnológica PLASA.</b>",
                    ConfigurationManager.AppSettings.Get("Url"));
            sTxtMail += string.Format("<br/>USUARIO: {0}<br/>CLAVE: {1}<br/>", user.Login,Seguridad.DesEncriptar(user.Clave));
            return sTxtMail;
        }

        public static string PieCorreo(string sTxtMail)
        {
            sTxtMail +=
                string.Format(
                    "<br/><br/>Este e-mail es autogenerado por favor no responder al mismo. <br/><br/>Dirección de Medio Ambiente<br/>GOBIERNO PROVINCIAL DEL GUAYAS");
            return sTxtMail;
        }
        //public void RegistrarLog( object log)
        //{
        //    var rutaLog = MapPath("~/Resources/log.txt");
            
        //    if (ConfigurationManager.AppSettings.Get("Debug") == "true")
        //    {
        //        Window win = new Window
        //        {
        //            ID = "winLog",Title = "Log",Height = 185,Width = 350,BodyPadding = 5,Modal = true,AutoScroll = true,AutoRender = false,Collapsible = true,Maximizable = true,
                    
                  
                    
        //            CloseAction = CloseAction.Destroy,
        //            Html = log.ToString()
        //        };
        //        win.Render();
        //        X.Js.Call("AppGlobalJs.showBug", log.ToString());
        //    }
        //    Util.RegistarLog(log,rutaLog);
           
            
        //}
        /// <summary>
        /// Cerrar Tabs
        /// </summary>
        /// <param name="tp"></param>
        public void CloseTabs(TabPanel tp,int indexIni)
        {
            var tabs =tp.Items;
            var begin = tabs.Count()-1;
            for (int i = begin;  i>= indexIni; i--)
            {
                //tabs[i].FireEvent("close");
                tp.CloseTab(i, CloseAction.Hide);
            }
            //if (indexIni==1)
            //tp.SetActiveTab(0);
           
        }
        /// <summary>
        /// ~/Licenciamiento/promotor/tabs/wucLoadDoc.ascx
        /// ~/Licenciamiento/admin/tabs/wucTareas.ascx
        /// </summary>
        /// <param name="panel">Panel contine wuc</param>
        /// <param name="idPage">Id de pagina</param>
        /// <param name="idFase">Id de fase</param>
        public static void SetCustomConfigWucLicAll(Ext.Net.Panel panel, string idPage, string idFase)
        {
            if (panel.CustomConfig.Count == 0)
            {
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_faseLic", idPage + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_fase", idPage + "FASE_ID_" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_page", idPage, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoFase", idPage + "_estadoFase" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_exp", idPage + Constantes.Expediente, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase", idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codFase", idPage + "codFase_" + idFase, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucDocAud(Ext.Net.Panel panel, string idPage)
        {
            if (panel.CustomConfig.Count == 0)
            {

                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_page", idPage, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoAud", idPage + "_estadoAud", ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_exp", idPage + Constantes.Expediente, ParameterMode.Value));


            }
        }
        public void SetCustomConfigWucLicInforme(Ext.Net.Panel panel, string idPage, string idFase, string session)
        {
            if (panel.CustomConfig.Count == 0)
            {
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_faseLic", idPage + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_fase", idPage + "FASE_ID_" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codFase", idPage + "codFase_" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_page", idPage, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_session", session, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoFase", idPage + "_estadoFase" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase", idFase, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucFaseLic(Ext.Net.Panel panel,string idPage, string idFase)
        {
            string name = panel.Parent.ID + "_faseLic";
            if (!panel.CustomConfig.Contains(name))
            {
                panel.CustomConfig.Add(new ConfigItem(name, idPage + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoFase", idPage + "_estadoFase" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase", idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codFase", idPage + "codFase_" + idFase, ParameterMode.Value));
            }
        }
        public static void SetCustomConfig(Ext.Net.FormPanel frm, string name, string val)
        {
            if (!frm.CustomConfig.Contains(name))
            {
                frm.CustomConfig.Add(new ConfigItem(name, val, ParameterMode.Value));
            }
           
        }
        public static void SetCustomConfig(Ext.Net.Panel panel, string name,string val)
        {
            if (!panel.CustomConfig.Contains(name))
            {
                panel.CustomConfig.Add(new ConfigItem(name, val, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucPage(Ext.Net.Panel panel, string idPage)
        {
            string name = panel.Parent.ID + "_page";
            if (!panel.CustomConfig.Contains(name))
            {
                panel.CustomConfig.Add(new ConfigItem(name, idPage, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucPage(Ext.Net.FormPanel frm, string idPage)
        {
            string name = frm.Parent.ID + "_page";
            if (!frm.CustomConfig.Contains(name))
            {
                frm.CustomConfig.Add(new ConfigItem(name, idPage, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucAud(Ext.Net.Panel panel, string fase, string tabla, string page)
        {
            if (panel.CustomConfig.Count == 0)
            {
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_fase", page + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_AexpId", page + Constantes.Expediente, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_aud", tabla, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_page", page, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase", fase, ParameterMode.Value));
               
            }
        }
        public static void SetCustomConfigWucAllAud(Ext.Net.Panel panel, string fase, string tabla, string page,string idAud,string idExp,string idExpAud)
        {
            if (panel.CustomConfig.Count == 0)
            {
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_faseAud", page + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_fase", page + "FASE_ID_" + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codFase", page + "codFase_" + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_AexpId", idExp, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_aud", tabla, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_page", page, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_id_aud", idAud, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoFase", page + "_estadoFase" + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase",fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_exa_exp", idExpAud, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigResumen(Ext.Net.Panel panel, string idPage, string jefatura)
        {
            if (panel.CustomConfig.Count == 0)
            {
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_exp", idPage + Constantes.Expediente, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_jefatura", jefatura, ParameterMode.Value));
            }
        }
        public void GetNameFile(FileUploadField myCtlFileUp, string fileExt, ref string ruta, ref string fileName, ref string fileNameTmp)
        {
            do
            {
                Random r = new Random(DateTime.Now.Millisecond);
                ruta = string.Format("~/{0}/{1}{2}", ConfigurationManager.AppSettings.Get("PathFiles"), r.Next(100, 999).ToString(), myCtlFileUp.PostedFile.FileName);
                fileName = MapPath(ruta).Trim();
                fileNameTmp = MapPath(string.Format("~/{0}/{1}{2}.{3}", ConfigurationManager.AppSettings.Get("PathFiles"), r.Next(100, 999).ToString(), "tmp", fileExt)).Trim();
            } while (File.Exists(fileName));
        }
       
        
    }
    public class ucWuc : UserControl
    {

        public void RegistrarLog(object log, bool mostrarVentana = false, string mensaje = "")
        {
            var rutaLog = MapPath("~/Resources/log.txt");
            if (ConfigurationManager.AppSettings.Get("Debug") == "true")
            {
                Window win = new Window
                {
                    ID = "winLog",
                    Title = "Log",
                    Height = 185,
                    Width = 350,
                    BodyPadding = 5,
                    Modal = true,
                    AutoScroll = true,
                    CloseAction = CloseAction.Destroy,
                    Html = log.ToString()
                };
                win.Render();
                X.Js.Call("AppGlobalJs.showBug", log.ToString());
            }
            Util.RegistarLog(log, rutaLog);

            if (mostrarVentana)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR,
                    Title = Titulos.TituloAviso,
                    Message = mensaje
                });
            }
        }
        public void UpdateCustomConfig(Ext.Net.Panel pnl, string name, string value)
        {
            if (pnl.CustomConfig.Contains(name))
            {
                ConfigItem varItemConfig = pnl.CustomConfig.FirstOrDefault(r => r.Name == name);
                int index = pnl.CustomConfig.IndexOf(varItemConfig);
                pnl.CustomConfig.RemoveAt(index);
                pnl.CustomConfig.Add(new ConfigItem(name, value));
            }
        }
        private static string readCustomConfig(Ext.Net.Panel pnl, string idWuc, string configName)
        {
            var _myValDinamic = "";
            string sName = idWuc + configName;
            if (pnl.CustomConfig.Contains(sName))
            {
                ConfigItem select = pnl.CustomConfig.FirstOrDefault(r => r.Name == sName);
                _myValDinamic = select.Value;
            }
            return (_myValDinamic);
        }
        private static string readCustomConfig(Ext.Net.FormPanel frm, string idWuc, string configName)
        {
            var _myValDinamic = "";
            string sName = idWuc + configName;
            if (frm.CustomConfig.Contains(sName))
            {
                ConfigItem select = frm.CustomConfig.FirstOrDefault(r => r.Name == sName);
                _myValDinamic = select.Value;
            }
            return (_myValDinamic);
        }
        public string ReadCustomConfig(Ext.Net.Panel pnl, string idWuc, string configName)
        {
            return readCustomConfig(pnl, idWuc, configName);
        }
        public string ReadCustomConfig(Ext.Net.FormPanel frm, string idWuc, string configName)
        {
            return readCustomConfig(frm, idWuc, configName);
        }
        public string ReadCustomConfigFaseLic(Ext.Net.Panel pnl, string idWuc)
        {

            string configName =  "_faseLic";
            return readCustomConfig(pnl, idWuc, configName);
        }

        public string ReadCustomConfigSessionCodigoFase(Ext.Net.Panel pnl, string idWuc)
        {
            string configName =  "_codFase";
            return readCustomConfig(pnl, idWuc, configName);
        }
        public string ReadCustomConfigCodeFase(Ext.Net.Panel frm, string idWuc)
        {

            string configName = "_codeFase";
            return readCustomConfig(frm, idWuc, configName);
        }
        public string ReadCustomConfigFaseAuditoria(Ext.Net.Panel pnl, string idWuc)
        {
            string configName = "_faseAud";
            return readCustomConfig(pnl, idWuc, configName);
          
        }
        public string ReadCustomConfigEstadoAuditoria(Ext.Net.Panel pnl, string idWuc)
        {
            string configName = "_estadoAud";
            return readCustomConfig(pnl, idWuc, configName);

        }
        public string ReadCustomConfigFase(Ext.Net.Panel pnl, string idWuc)
        {
            string configName = "_fase";
            return readCustomConfig(pnl, idWuc, configName);

        }
        public string ReadCustomConfigAuditoriaId(Ext.Net.Panel pnl, string idWuc)
        {
            string configName = "_id_aud";
            return readCustomConfig(pnl, idWuc, configName);
        }
        public string ReadCustomConfigPage(Ext.Net.Panel pnl, string idWuc)
        {
            string configName = "_page";          
            return readCustomConfig(pnl, idWuc, configName);
        }
        public string ReadCustomConfigPage(Ext.Net.FormPanel frm, string idWuc)
        {

            string configName = "_page";
            return readCustomConfig(frm, idWuc, configName);
        }
        public string ReadCustomConfigExpediente(Ext.Net.Panel frm, string idWuc)
        {

            string configName = "_exp";
            return readCustomConfig(frm, idWuc, configName);
        }
        //_exa_exp
        public string ReadCustomConfigExpedienteAuditoria(Ext.Net.Panel frm, string idWuc)
        {

            string configName = "_exa_exp";
            return readCustomConfig(frm, idWuc, configName);
        }
        public string ReadCustomConfigValAux(Ext.Net.Panel frm, string idWuc)
        {

            string configName = "_valaux";
            return readCustomConfig(frm, idWuc, configName);
        }
        
        public string ReadCustomConfigEstadoFase(Ext.Net.Panel pnl, string idWuc)
        {
            string configName = "_estadoFase";
            return readCustomConfig(pnl, idWuc, configName);
        }
        public string ReadCustomConfigEstadoFase(Ext.Net.FormPanel frm, string idWuc)
        {

            string configName = "_estadoFase";
            return readCustomConfig(frm, idWuc, configName);
        }
        public string ReadCustomConfigSession(Ext.Net.Panel pnl, string idWuc)
        {

            string configName = "_session";
            return readCustomConfig(pnl, idWuc, configName);
        }
        public string ReadCustomConfigValJefatura(Ext.Net.Panel pnl, string idWuc)
        {
            string configName = "_jefatura";
            return readCustomConfig(pnl, idWuc, configName);
        }
        /// <summary>
        /// ~/Licenciamiento/promotor/tabs/wucLoadDoc.ascx
        /// ~/Licenciamiento/admin/tabs/wucTareas.ascx
        /// </summary>
        /// <param name="panel">Panel contine wuc</param>
        /// <param name="idPage">Id de pagina</param>
        /// <param name="idFase">Id de fase</param>
       
        public static void SetCustomConfigWucLicAll(Ext.Net.Panel panel, string idPage, string idFase)
        {
            if (panel.CustomConfig.Count == 0)
            {
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_faseLic", idPage + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_fase", idPage + "FASE_ID_" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_page", idPage, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoFase", idPage + "_estadoFase" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_exp", idPage + Constantes.Expediente, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase", idFase, ParameterMode.Value));
            }
        }

       
        public static void SetCustomConfigWucPage(Ext.Net.Panel panel, string idPage)
        {
            string name = panel.Parent.ID + "_page";
            if (!panel.CustomConfig.Contains(name))
            {
                panel.CustomConfig.Add(new ConfigItem(name, idPage, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucPage(Ext.Net.FormPanel frm, string idPage)
        {
            string name = frm.Parent.ID + "_page";
            if (!frm.CustomConfig.Contains(name))
            {
                frm.CustomConfig.Add(new ConfigItem(name, idPage, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucValAux(Ext.Net.Panel panel, string idPage)
        {
            string name = panel.Parent.ID + "_valaux";
            if (!panel.CustomConfig.Contains(name))
            {
                panel.CustomConfig.Add(new ConfigItem(name, idPage, ParameterMode.Value));
            }
        }
        public void GetNameFile(FileUploadField myCtlFileUp, string fileExt, ref string ruta, ref string fileName, ref string fileNameTmp)
        {
            do
            {
                string sNameTmp = "";
                Random r = new Random(DateTime.Now.Millisecond);
                sNameTmp = string.Format("~/{0}/{1}{2}", ConfigurationManager.AppSettings.Get("PathFiles"), r.Next(100, 999).ToString(), myCtlFileUp.PostedFile.FileName);
                ruta = new Regex(@"\s*").Replace(sNameTmp, string.Empty);
                //Quitar espacios blanco
                fileName = new Regex(@"\s*").Replace(MapPath(ruta).Trim(), string.Empty);
                sNameTmp = MapPath(string.Format("~/{0}/{1}{2}.{3}", ConfigurationManager.AppSettings.Get("PathFiles"), r.Next(100, 999).ToString(), "tmp", fileExt)).Trim();
                fileNameTmp = new Regex(@"\s*").Replace(sNameTmp, string.Empty);
            } while (File.Exists(fileName));
        }
        public static void SetCustomConfigWucAllAud(Ext.Net.Panel panel, string fase, string tabla, string page, string idAud, string idExp, string idExpAud)
        {
            if (panel.CustomConfig.Count == 0)
            {
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_faseAud", page + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_fase", page + "FASE_ID_" + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codFase", page + "codFase_" + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_AexpId", idExp, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_aud", tabla, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_page", page, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_id_aud", idAud, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoFase", page + "_estadoFase" + fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase", fase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_exa_exp", idExpAud, ParameterMode.Value));
            }
        }
        public static void SetCustomConfigWucFaseLic(Ext.Net.Panel panel, string idPage, string idFase)
        {
            string name = panel.Parent.ID + "_faseLic";
            if (!panel.CustomConfig.Contains(name))
            {
                panel.CustomConfig.Add(new ConfigItem(name, idPage + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_estadoFase", idPage + "_estadoFase" + idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codeFase", idFase, ParameterMode.Value));
                panel.CustomConfig.Add(new ConfigItem(panel.Parent.ID + "_codFase", idPage + "codFase_" + idFase, ParameterMode.Value));
            }
        }
        /// <summary>
        /// Remover las ultimas n columnas de la grid
        /// </summary>
        /// <param name="n"></param>
        /// <param name="grid"></param>
        public void RemoveEndColumnGridGlobal(int n, GridPanel grid)
        {
            int index = grid.ColumnModel.Columns.Count;
            //No vizualizar las ultimas n columnas
            for (int i = n; i > 0; i = i - 1)
            {
                index -= 1;
                grid.RemoveColumn(index);
            }
        }

    }
   
    /// Esta clase contiene funciones para encriptar/desencriptar
    /// El ser estática no es necesario instanciar un objeto para 
    /// usar las funciones Encriptar y DesEncriptar
    ///http://revistadigital.inesem.es/informatica-comunicaciones/seguridad-en-c-encriptar-y-desencriptar-datos/
    public static class Seguridad
    {

        /// Encripta una cadena
        public static string Encriptar(this string _cadenaAencriptar)
        {
            string result = string.Empty;
            byte[] encryted = System.Text.Encoding.Unicode.GetBytes(_cadenaAencriptar);
            result = Convert.ToBase64String(encryted);
            return result;
        }

        /// Esta función desencripta la cadena que le envíamos en el parámentro de entrada.
        public static string DesEncriptar(this string _cadenaAdesencriptar)
        {
            string result = string.Empty;
            byte[] decryted = Convert.FromBase64String(_cadenaAdesencriptar);
            //result = System.Text.Encoding.Unicode.GetString(decryted, 0, decryted.ToArray().Length);
            result = System.Text.Encoding.Unicode.GetString(decryted);
            return result;
        }
    }
}